package com.dyb.recuitment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyb.recuitment.entity.Setting;
import com.dyb.recuitment.entity.Shield;
import com.dyb.recuitment.mapper.ShieldMapper;
import com.dyb.recuitment.service.IShieldService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 屏蔽表 服务实现类
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-21
 */
@Service
public class ShieldServiceImpl extends ServiceImpl<ShieldMapper, Shield> implements IShieldService {

    @Autowired
    private ShieldMapper shieldMapper;

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public int add(Shield shield) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        shield.setCreateTime(localDateTime).setUpdateTime(localDateTime);
        Shield exist_shield = new Shield();
        exist_shield.setUserId(shield.getUserId()).setCompanyId(shield.getCompanyId()).setStatus(1);
        List<Shield> exist_shields = selectByShield(exist_shield);
        if (exist_shields.size() <= 0) {
            return shieldMapper.insert(shield);
        }
        return 0;
    }

    @Override
    public int delete(Shield shield) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        shield.setStatus(0).setUpdateTime(localDateTime);
        return shieldMapper.updateById(shield);
    }

    @Override
    public List<Shield> selectByShield(Shield shield) {
        shield.setStatus(1);
        QueryWrapper<Shield> queryWrapper = new QueryWrapper<>(shield);
        return shieldMapper.selectList(queryWrapper);
    }

    @Override
    public List<Shield> selectAllShield() {
        return shieldMapper.selectList(new QueryWrapper<>());
    }
}
